package com.zzk.audiovideodemo.util;

import android.content.Context;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: AudioVideoDemo
 * @Package: com.zzk.audiovideodemo.util
 * @ClassName: VideoRecordRefresh
 * @Description:
 * @Author: brilliantzhao
 * @CreateDate: 2021.4.19 16:09
 * @UpdateUser:
 * @UpdateDate: 2021.4.19 16:09
 * @UpdateRemark:
 * @Version: 1.0.0
 */

public class VideoRecordRefresh {

    private ArrayList<String> mListPathSysImg = new ArrayList<>();//通知系统图库专用
    private static String TAG = "VideoRecord";

    public static void galleryRefresh(Context context, String filePath) {
//        File file = new File(filePath);
//        if (file.exists()) {
//            Uri uri = Uri.fromFile(file);
//            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//            intent.setData(uri);
//            context.sendBroadcast(intent);
//        }

        MediaScannerConnection.scanFile(context, new String[]{filePath}, null, null);

        //strDir视频路径
        Uri localUri = Uri.parse("file://" + filePath);
        Intent localIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
        localIntent.setData(localUri);
        context.sendBroadcast(localIntent);

        getPathList(true);
    }


    /**
     * 获得所有图片或者视频的路径
     *
     * @param isVideo
     * @return
     */
    public static List<String> getPathList(boolean isVideo) {
        List pathList = new ArrayList<>();
        File videoDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "infisense/video");
        String pathStrig;
        if (isVideo) {
            pathStrig = videoDir.getAbsolutePath();         //视频文件夹的途径
        } else {
            pathStrig = videoDir.getAbsolutePath();         //图片文件夹的路径
        }
        Log.e(TAG, "isVideo" + isVideo);
        File media = new File(pathStrig);
        Log.e(TAG, "media是否是文件夹" + media.isDirectory());
        if (media.isDirectory()) {
            File[] files = media.listFiles();
            Log.e(TAG, "文件数量" + files.length);
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (file.exists()) {
                    pathList.add(file.getAbsolutePath());
                }
            }
        }
        Log.e(TAG, "图片或者视频的数量：" + pathList.size());
        return pathList;
    }

}
